package cn.com.shopec.erp.purchase.commo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.purchase.entity.PurchaseOrder;
import cn.com.shopec.erp.purchase.entity.PurchaseRequisitionProduct;
import cn.com.shopec.erp.purchase.entity.PurchaseReturnOrder;
import cn.com.shopec.erp.purchase.service.IPurchaseRequisitionService;
import cn.com.shopec.erp.purchase.service.IPurchaseReturnOrderService;
import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;
import cn.com.shopec.erp.purchase.vo.PurchaseReturnOrderVo;
import cn.com.shopec.erp.system.common.UserUtils;

/**
 * 
 * @ClassName: DefectiveConvertor
 * @Description: 采购对象转换器
 * @author: LiangShunWang
 * @date 2016-11-21 下午17:16:22
 * 
 */
@Component
public class PurchaseVoReturnConvertor {
	
	@Resource
	private IPurchaseReturnOrderService poReturnService;
	
	@Resource
	private IPurchaseRequisitionService purchaseRequistionService;
	
	
	 public List<PurchaseReturnOrderVo> convert2DefectiveVos(List<PurchaseReturnOrder> purchaseList) {
	        List<PurchaseReturnOrderVo> purchaseVos = null;

	        if (LogicUtil.isNotNullAndEmpty(purchaseList)) {
	        	purchaseVos = new ArrayList<PurchaseReturnOrderVo>();

	            for (PurchaseReturnOrder purchaseOrder : purchaseList) {
	            	PurchaseReturnOrderVo purchaseVo = this.convert2DefectiveVo(purchaseOrder);
	            	purchaseVos.add(purchaseVo);
	            }
	        }

	        return purchaseVos;
	    }

	 public PageFinder<PurchaseReturnOrderVo> convert2PagedDefectiveVos(PageFinder<PurchaseReturnOrder> pagedBrands) {
	        PageFinder<PurchaseReturnOrderVo> pagedBrandVos = null;

	        if (LogicUtil.isNotNull(pagedBrands.getData())) {
	            List<PurchaseReturnOrderVo> defectiveVos = this.convert2DefectiveVos(pagedBrands.getData());

	            pagedBrandVos = new PageFinder<PurchaseReturnOrderVo>(pagedBrands.getPageNo(), pagedBrands.getPageSize(),
	                    pagedBrands.getRowCount(), defectiveVos);
	        }

	        return pagedBrandVos;
	    }
	 
	 public PurchaseReturnOrderVo convert2DefectiveVo(PurchaseReturnOrder purchaseOrder) {
		 PurchaseReturnOrderVo purchaseOrderVo = null;
	        if (LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrderVo = new PurchaseReturnOrderVo();
	        	purchaseOrderVo.setPuNo(purchaseOrder.getPuNo());
	        	purchaseOrderVo.setId(purchaseOrder.getId());
	        	purchaseOrderVo.setPoNo(purchaseOrder.getPoNo());
	        	purchaseOrderVo.setProdNo(purchaseOrder.getProdNo());
	        	purchaseOrderVo.setCommodityName(purchaseOrder.getCommodityName());
	        	purchaseOrderVo.setSpecification(purchaseOrder.getSpecification());
	        	purchaseOrderVo.setSupCode(purchaseOrder.getSupCode());
	        	purchaseOrderVo.setSupName(purchaseOrder.getSupName());
	        	purchaseOrderVo.setPurWhNo(purchaseOrder.getPurWhNo());
	        	purchaseOrderVo.setPurWhName(purchaseOrder.getPurWhName());
	        	purchaseOrderVo.setReturnQuantity(purchaseOrder.getReturnQuantity());
	        	purchaseOrderVo.setTotalPrice(purchaseOrder.getTotalPrice());
	        	purchaseOrderVo.setPurUnitPrice(purchaseOrder.getPurUnitPrice());
	        	purchaseOrderVo.setReturnDesc(purchaseOrder.getReturnDesc());
	        	purchaseOrderVo.setCreater(purchaseOrder.getCreater());
	        	purchaseOrderVo.setCreateTime(purchaseOrder.getCreateTime());
	        	purchaseOrderVo.setUpdateTime(purchaseOrder.getUpdateTime());
	        	purchaseOrderVo.setStatus(purchaseOrder.getStatus());
	        	purchaseOrderVo.setDeleteFlag(purchaseOrder.getDeleteFlag());
	        	purchaseOrderVo.setLastAuditTime(purchaseOrder.getLastAuditTime());
	        	purchaseOrderVo.setAuditResult(purchaseOrder.getAuditResult());
	        	purchaseOrderVo.setCurAuditPoint(purchaseOrder.getCurAuditPoint());
	        	purchaseOrderVo.setDescription(purchaseOrder.getDescription());
	        	purchaseOrderVo.setLastAuditPerson(purchaseOrder.getLastAuditPerson());
	        	purchaseOrderVo.setRemark(purchaseOrder.getRemark());
	        	purchaseOrderVo.setYieldAmountNumner(purchaseOrder.getYieldAmountNumner());
	        	purchaseOrderVo.setDefectiveAmountNumber(purchaseOrder.getDefectiveAmountNumber());
	        	purchaseOrderVo.setCommoNo(purchaseOrder.getCommoNo());
	        	purchaseOrderVo.setProdBarCode(purchaseOrder.getProdBarCode());
	        	purchaseOrderVo.setPrId(purchaseOrder.getPrId());
	        	purchaseOrderVo.setPurType(purchaseOrder.getPurType());
	        }
	        return purchaseOrderVo;
	    }
	 
	 public PurchaseReturnOrder convert2Defective(PurchaseRequisitionProductVo purchaseOrderVo) {
		 PurchaseReturnOrder purchaseOrder = null;

	        if (LogicUtil.isNotNull(purchaseOrderVo)) {
	        	purchaseOrder = new PurchaseReturnOrder();
	        	purchaseOrder.setPoNo(purchaseOrderVo.getPoNo());
	        	purchaseOrder.setProdNo(purchaseOrderVo.getProductNo());
	        	purchaseOrder.setCommodityName(purchaseOrderVo.getPpName());
	        	purchaseOrder.setSpecification(purchaseOrderVo.getPpSpecification());
	        	purchaseOrder.setSupName(purchaseOrderVo.getPpSupplierName());
	        	purchaseOrder.setPurWhName(purchaseOrderVo.getPpWarehouseName());
	        	purchaseOrder.setReturnQuantity(purchaseOrderVo.getPpNumber());
	        	purchaseOrder.setTotalPrice(purchaseOrderVo.getTotalPrice());
	        	purchaseOrder.setPurUnitPrice(purchaseOrderVo.getPpUnitPrice());
	        	purchaseOrder.setTotalPrice(purchaseOrderVo.getTotalPrice());
	        	purchaseOrder.setDescription(purchaseOrderVo.getDescription());
	        	purchaseOrder.setReturnDesc(purchaseOrderVo.getReturnDesc());
	        	purchaseOrder.setSupCode(purchaseOrderVo.getPpSupplierNo());
	        	purchaseOrder.setPurWhNo(purchaseOrderVo.getPpWarehouseNo());
	        	purchaseOrder.setYieldAmountNumner(purchaseOrderVo.getYieldAmountNumner());
	        	purchaseOrder.setDefectiveAmountNumber(purchaseOrderVo.getDefectiveAmountNumber());
	        	purchaseOrder.setCommoNo(purchaseOrderVo.getCommodityNo());
	        	purchaseOrder.setProdBarCode(purchaseOrderVo.getProdBarCode());
	        	purchaseOrder.setPrId(purchaseOrderVo.getId());
	            if (purchaseOrderVo.getDeleteFlag() == null ) {
	            	purchaseOrder.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
	            } else {
	            	purchaseOrder.setDeleteFlag(purchaseOrderVo.getDeleteFlag());
	            }
	        }
	        return purchaseOrder;
	    }
	 
	 public PurchaseReturnOrder convert2Defective(PurchaseReturnOrderVo purchaseOrderVo, PurchaseReturnOrder purchaseOrder) {

	        if (LogicUtil.isNotNull(purchaseOrderVo) && LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrder.setLastAuditTime(DateUtil.formatTimesTampDate(DateUtil.getCurrentDate()));
	        	purchaseOrder.setLastAuditPerson(UserUtils.getLoginUserName());
	            return purchaseOrder;
	        } else {
	            return null;
	        }
	    }
	
	 public PurchaseReturnOrder convert2DefectiveUpdateAuditResult(String purchaseNo, PurchaseReturnOrder purchaseOrder,int status) throws Exception {
		 
	        if (LogicUtil.isNotNull(purchaseNo) && LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrder.setStatus(status);
	        	//修改采购单实体为可持续退货
	        	if(purchaseRequistionService.updateAuditResultPurchaseRequestion(purchaseOrder.getPoNo(), purchaseOrder.getProdNo())){
	        		return  null;
	        	}
	        	if(status == ProductSCMConstant.PURCHASE_RETURN_STATUS && purchaseOrder.getPurType() == ProductSCMConstant.PUR_RETURN_TYPE_TWO){
	        		purchaseOrder.setStatus(ProductSCMConstant.PURCHASERETURN_STATUS_FOUR);
	        		purchaseOrder.setCurAuditPoint(ProductSCMConstant.PURCHASERETURN_CURAUDIT_SUCCESS);
	        		/*if(!purchaseRequistionService.updateAuditResultPurchaseRequestion(purchaseNo,purchaseOrder.getProdNo())){
	        			return null;
	        		}else{
	        			purchaseOrder.setUpdateTime(DateUtil.formatTimesTampDate(DateUtil.getCurrentDate()));
	    	            return purchaseOrder;
	        		}*/
	        		PurchaseReturnOrder purchaseReturnOrder = 	poReturnService.getPuNoByPurchaseRequest(purchaseNo,purchaseOrder.getProdNo());
	        		if(purchaseReturnOrder!=null){
	        			purchaseOrder.setUpdateTime(DateUtil.formatTimesTampDate(DateUtil.getCurrentDate()));
	    	            return purchaseOrder;
	        		}else{
	        			return null;
	        		}
	        		
	        		
	        	}
	            return purchaseOrder;
	        } else {
	            return null;
	        }
	    }
	 
	 /**
	  * 改变采购单的状态为：采购中
	  * 
	  */
	 public PurchaseOrder convert2DefectiveList(String poNo, PurchaseOrder purchaseOrder) {

	        if (LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrder.setAuditResult(ProductSCMConstant.LASTAUDITRESULT_OK);
	        	purchaseOrder.setPoStatus(ProductSCMConstant.PO_STATUS_THREE);
	            return purchaseOrder;
	        } else {
	            return null;
	        }
	    }
}
